package net.sevear.chaos.douban.shuo
{
	import com.adobe.serialization.json.JSON;
	import com.douban.auth.DBAuthenticate;
	
	import de.popforge.utils.SprintfUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.graphics.RadialGradient;
	
	import net.sevear.chaos.douban.DBShuoManager;
	import net.sevear.chaos.utils.Logger;
	import net.sevear.chaos.vo.UserInfo;
	
	public class DBShuoFollow extends EventDispatcher
	{
		public static const PAGE_COUNT:int = 5;
		private static const FOLLOWLIST_URI:String = "/api/users/%s/following?count=%d&page=%d";
		private var _requestPageIdx:int = -1;
		private var _total:int = 0;
		private var loader:URLLoader;
		
		public function DBShuoFollow()
		{
			super(null);
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,onLoadComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onIOErr);
		}
		
		public function set total(value:int):void{
			_total = value;
		}
		
		public function get total():int{
			return _total;
		}
		
		public function loadContactList(pageIdx:int):void{
			if(pageIdx>=0 && pageIdx*PAGE_COUNT<_total+PAGE_COUNT){
				_requestPageIdx = pageIdx;
			}
			
			try{
				loader.close();
			}catch(e:Error){
				
			}
			
			var rst:URLRequest = new URLRequest(DBShuoManager.SHUO_SERVER+
				SprintfUtil.sprintf(FOLLOWLIST_URI,DBAuthenticate.getInstance().dbScreenName,PAGE_COUNT,pageIdx));
			loader.load(rst);
		}
		
		private function onLoadComplete(e:Event):void{
			var str:String = loader.data as String;
			var rstObj:Object = JSON.decode(str);
			if(rstObj is Array){
				var followlist:Array = parseResult(rstObj as Array);
				if(followlist.length>0){
					dispatchEvent(new DBShuoEvent(DBShuoEvent.GET_FOLLOW_LIST,
						{page:_requestPageIdx,result:followlist}));
				}
			}else{
				dispatchEvent(new DBShuoEvent(DBShuoEvent.GET_FOLLOW_LIST_FAILED,_requestPageIdx));
			}
		}
		
		private function parseResult(rst:Array):Array{
			var user:UserInfo;
			var list:Array = [];
			for each(var obj:Object in rst){
				user = new UserInfo();
				user.location = obj.city as String;
				user.dbName   = obj.screen_name as String;
				user.dbUID = obj.uid as String; 
				user.dbID = parseInt(obj.id as String);
				user.dbURL = "http://www.douban.com/people/"+user.dbUID+"/";
				user.imgUrl = obj.small_avatar as String;
				user.isActivate = UserInfo.ACTIVATE_UNKNOWN;
				user.lastActivitTime = null;
				user.selected = false;
				user.signature = obj.description as String;
				list.push(user);
			}
			
			
			return list;
		}
		
		private function onIOErr(e:IOErrorEvent):void{
			Logger.getInstance().logError("Error happened when get DouBan Shuo Follow List");
			dispatchEvent(new DBShuoEvent(DBShuoEvent.GET_FOLLOW_LIST_FAILED,_requestPageIdx));
		}
	}
}